#读入股票数据，写入save/stk.data.inter.intra4 文件中
read.stk<-function(){
  source('c:/R/script/sta.data.r')
  source('c:/R/script/gen.signal.r')

  #获取日间数据
  setwd('c:/R/data/')
  load("allstocktill20130715.RData")
  Stock.inter = allstocktill20130715[,c('time','symbol','CLOSE','VOL_SHARE','DIVIDEND', 'SPLIT', 'ADJ_FACTOR')]
  Stock.inter.list = split(Stock.inter, Stock.inter$symbol); #将指数数据按日期分割

  Stock.symbol = unique(Stock.inter$symbol)
  Stock.inter.date = unique(Stock.inter$time)
  Stock.inter.price = matrix(nrow=length(Stock.inter.date), ncol=length(Stock.symbol), dimnames = list(Stock.inter.date, Stock.symbol))
  Stock.inter.vol = matrix(nrow=length(Stock.inter.date), ncol=length(Stock.symbol), dimnames = list(Stock.inter.date, Stock.symbol))
  Stock.inter.sta = matrix(nrow=length(Stock.inter.date), ncol=length(Stock.symbol), dimnames = list(Stock.inter.date, Stock.symbol))
  
  for(i in (1:length(Stock.symbol)))
  #for(i in (1:10))
  {
      print(i)
	Stock.inter.date.one = Stock.inter.list[[Stock.symbol[i]]]$time
      date.sel = match(Stock.inter.date.one, Stock.inter.date)
	date.sel = date.sel[!is.na(date.sel)]
	#Stock.inter.price[date.sel,i] = Stock.inter[Stock.inter$symbol==Stock.symbol[i],]$CLOSE*Stock.inter[Stock.inter$symbol==Stock.symbol[i],]$ADJ_FACTOR
      #Stock.inter.vol[date.sel,i] = Stock.inter[Stock.inter$symbol==Stock.symbol[i],]$VOL_SHARE
	Stock.inter.price[date.sel,i] = Stock.inter.list[[Stock.symbol[i]]]$CLOSE*Stock.inter.list[[Stock.symbol[i]]]$ADJ_FACTOR
	Stock.inter.vol[date.sel,i] = Stock.inter.list[[Stock.symbol[i]]]$VOL_SHARE
  }

  #获取日内数据
  wd = getwd()
  setwd('z:/stk_intra/老数据/按股票分类,每个股票一个csv/stk_intra_BySTK_2005_2013年7月/')
  files = dir()  #获取所有文件
  Stock.intra.list = list() #以股票的symbol为index
  all.date = c()
  Stock.intra.symbol = substr(files, 1, 6)
  Stock.intra.sta.list = list()
  #for(i in (1:5))
  for(i in (1:length(Stock.symbol)))
  {
    print(i)
    Stock.intra<-read.csv(files[i], colClasses=c(NA,NA,NA,rep('NULL',6),NA,rep('NULL',2)), stringsAsFactors=FALSE)
    #Stock.intra.list[[as.character(Stock.intra$symbol[1])]] = Stock.intra
	Stock.intra.sta = sta.stock(Stock.intra) #分析日内数据，返回矩阵
	Stock.intra.sta.list[[as.character(Stock.intra.symbol[i])]] = Stock.intra.sta #每只股票对应一个日内数据
	all.date = unique(c(all.date, Stock.intra$date)) #合并有效日期
  }
  setwd(wd) 	
  
  #names(Stock.intra.sta) = Stock.intra.symbol
  # View(all.date)  #看一下总的日期序列

  #Stock.symbol = names(Stock.intra.sta.list)
  Stock.intra.sta = matrix(nrow=length(Stock.inter.date), ncol=length(Stock.symbol), dimnames = list(Stock.inter.date, Stock.symbol))
  for(i in (1:length(Stock.symbol)))
  {
      if(Stock.symbol[i]%in%names(Stock.intra.sta.list))
	{
	date.com = intersect(Stock.inter.date, rownames(Stock.intra.sta.list[[Stock.symbol[i]]]))
	date.sel1 = intersect(date.com, Stock.inter.date)
	date.sel2 = intersect(date.com, rownames(Stock.intra.sta.list[[Stock.symbol[i]]]))

	date.sel1 = date.sel1[!is.na(date.sel1)]
	date.sel2 = date.sel2[!is.na(date.sel2)]
	if(sum(is.na(date.sel))/length(date.sel)<0.3) #缺失数据不超过30%
 		Stock.inter.sta[date.sel1, i] = Stock.intra.sta.list[[Stock.symbol[i]]][date.sel2,1] #只存该股票有效日期的数据 
	}
  }
  
  save(Stock.intra.sta, file='../save/stk.intra')
  save(Stock.inter.list, Stock.inter.price, Stock.inter.vol, file='save/stk.inter')
}

#读入指数数据，写入save/stk.data.inter.intra4 文件中
read.idx<-function(){
  #合并两年的指数数据
  sh300.2006 = read.csv("index\\sh000300_2006.csv", colClasses=c('NULL',NA,NA,NA,rep('NULL',15)), stringsAsFactors=FALSE) #只读取 证券代码 时间 最新 
  sh300.2007 = read.csv("index\\sh000300_2007.csv", colClasses=c('NULL',NA,NA,NA,rep('NULL',15)), stringsAsFactors=FALSE) #只读取 证券代码 时间 最新 
  sh300.2008 = read.csv("index\\sh000300_2008.csv", colClasses=c('NULL',NA,NA,NA,rep('NULL',15)), stringsAsFactors=FALSE) #只读取 证券代码 时间 最新 
  sh300.2009 = read.csv("index\\sh000300_2009.csv", colClasses=c('NULL',NA,NA,NA,rep('NULL',15)), stringsAsFactors=FALSE) #只读取 证券代码 时间 最新 
  idx= rbind(sh300.2006, sh300.2007, sh300.2008, sh300.2009)
  rm(sh300.2006, sh300.2007, sh300.2008, sh300.2009)
  
  #将时间拆分，然后绑定在一起
  idx.time.2 = do.call('rbind',strsplit(idx$时间,' ', fixed=TRUE)) ; 
  idx$date = idx.time.2[,1]
  idx$time = idx.time.2[,2]
  idx.intra.list <<- split(idx.intra.list, idx.intra.list$date); #将指数数据按日期分割
  
  #日间数据
  load('csi.RData')
  idx.inter = csi
  save(idx, idx.inter, file='save/idx.intra.inter')
}
